<?php

namespace app\api\model\cps;



use app\api\model\ExtendConsume;
use app\api\model\UserConsumeLog;
use app\common\model\User;
use app\shopapi\model\ExtendConsumeShop;
use fast\Http;

class Wph
{

    public $apikey = 'B128mKCQC6bsOxMB2pTm5GRJi15JWgos';

    //商品列表
    public function goods($page,$jxCode)
    {
        $api = "http://api.tbk.dingdanxia.com/vip/goodsList";

        $data = [
            'apikey'=>$this->apikey,
            'page'=>$page,
            'channelType'=>1,
            'sourceType'=>1,
            'jxCode'=>$jxCode
        ];

        $res = Http::sendRequest($api,$data);

        $result = json_decode($res['msg'],true);

        if ($result['code']==200){
            $list = $result['data'];
            foreach ($list as $k=>$v){
                $list[$k]['wawa_commission'] = round($v['commission']*0.56,2);
            }
            return $list;
        } else {
            throw new \Exception($result['msg']);
        }

    }


    //商品详情
    public function goodsdetail($id)
    {
        $api = "http://api.tbk.dingdanxia.com/vip/item_info";

        $data = [
            'apikey'=>$this->apikey,
            'id'=>$id,
        ];

        $res = Http::sendRequest($api,$data);

        $result = json_decode($res['msg'],true);

        if ($result['code']==200){
            $detail = $result['data'];
            $detail['wawa_commission'] =  round($detail['commission']*0.56,2);;
            return $detail;
        } else {
            throw new \Exception($result['msg']);
        }

    }


    //搜索商品列表
    public function searchgoods($page,$keyword)
    {
        $api = "http://api.tbk.dingdanxia.com/vip/query";

        $data = [
            'apikey'=>$this->apikey,
            'page'=>$page,
            'keyword'=>$keyword
        ];

        $res = Http::sendRequest($api,$data);

        $result = json_decode($res['msg'],true);

        if ($result['code']==200){
            $list = $result['data'];
            foreach ($list as $k=>$v){
                $list[$k]['wawa_commission'] = round($v['commission']*0.56,2);
            }
            return $list;
        } else {
            throw new \Exception($result['msg']);
        }

    }


    //商品转链
    public function url($id,$user_id)
    {
        $api = "http://api.tbk.dingdanxia.com/vip/id_privilege";

        $data = [
            'apikey'=>$this->apikey,
            'id'=>$id,
            'chanTag'=>$user_id
        ];

        $res = Http::sendRequest($api,$data);

        $result = json_decode($res['msg'],true);

        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['msg']);
        }
    }

    public function viplinkCheck($url)
    {
        $api = "http://api.tbk.dingdanxia.com/vip/viplink_check";
        $data = [
            'apikey'=>$this->apikey,
            'url'=>$url,
        ];

        $res = Http::sendRequest($api,$data);

        $result = json_decode($res['msg'],true);

        if ($result['code']==200){
            return [
                'code'=>1,
                'msg'=>'success',
                'data'=>$result['data']
            ];
        } else {
            return [
                'code'=>0,
                'msg'=>$result['msg'],
            ];
            //throw new \Exception($result['msg']);
        }
    }

    //商品链接转链
    public function urlPrivilege($url,$user_id)
    {
        $api = "http://api.tbk.dingdanxia.com/vip/url_privilege";
        preg_match_all('/https?:\/\/[^\s]+/i', $url, $matches);

        $urls = $matches[0];

        $goods_info = $this->viplinkCheck($urls[0]);
        if ($goods_info['code']==0){
            return [
                'code'=>0,
                'msg'=>$goods_info['msg'],
            ];
        }
        $itemInfo = json_encode($goods_info['data'],320);
        //dump($goods_info);die;
        $data = [
            'apikey'=>$this->apikey,
            'url'=>$urls[0],
            'chanTag'=>$user_id,
            'itemInfo'=>$itemInfo
        ];

        $res = Http::sendRequest($api,$data);

        $result = json_decode($res['msg'],true);

        if ($result['code']==200){
            $commission = $result['data']['itemInfo']['commission'];
            $result['data']['original_commission'] = $commission;
            $result['data']['commission'] = round($commission*0.56,2);
            $data = $result['data'];
            return [
                'code'=>1,
                'msg'=>'success',
                'data'=>$data
            ];
        } else {
            return [
                'code'=>0,
                'msg'=>$result['msg'],
            ];
            //throw new \Exception($result['msg']);
        }
    }

    //查询未结算的订单
    public function unliquidated()
    {
        $lists = UserConsumeLog::where(['cps_type'=>2,'consume_type'=>2,'is_to_account'=>0,'is_common'=>0])
            ->field('pay_time')->select();

        foreach ($lists as $k=>$v){
            $start = ($v['pay_time']-3500)*1000;
            $end = $v['pay_time']*1000;
            $res = $this->order(1,$start,$end);
        }
    }


    //订单列表
    public function order($type=0,$start='',$end='')
    {
        $api = "http://api.tbk.dingdanxia.com/vip/order_details2";

        $orderTimeStart = (time()-3500)*1000;
        $orderTimeEnd = time()*1000;
        $updateTimeStart = (time()-3500)*1000;
        $updateTimeEnd =time()*1000;

        if ($type==1){
            $orderTimeStart = $start;
            $orderTimeEnd = $end;
            $updateTimeStart = $start;
            $updateTimeEnd = $end;
        }

        $data = [
            'apikey'=>$this->apikey,
            'page'=>1,
            'pageSize'=>100,
            'orderTimeStart'=>$orderTimeStart,
            'orderTimeEnd'=>$orderTimeEnd,
            'updateTimeStart'=>$updateTimeStart,
            'updateTimeEnd'=>$updateTimeEnd,
        ];
        //dump($data);die;
        $res = Http::sendRequest($api,$data);

        $result = json_decode($res['msg'],true);

        if ($result['code']==200){
            $list =  $result['data'];
            //dump($list);die;
            foreach ($list as $k=>$v){
                $user_id = $v['channelTag'];
                $user = User::get($user_id);
                if ($user){
                    $promotion_amount = $v['commission'];

                    $commission = $promotion_amount*0.56;
                    //增加消费记录
                    $consume_log_info = UserConsumeLog::get([ 'user_id'=>$user_id, 'order_no'=>$v['orderSn']]);
                    if (!$consume_log_info){
                        $consume_log_model = new UserConsumeLog();
                        $consume_log_data = [
                            'user_id'=>$user_id,
                            'title'=>$v['detailList'][0]['goodsName'],
                            'shop_id'=>$v['detailList'][0]['brandStoreSn'],
                            'shop_name'=>$v['detailList'][0]['brandStoreName'],
                            'image'=>$v['detailList'][0]['goodsThumb'],
                            'order_no'=>$v['orderSn'],
                            'pay_time'=>$v['orderTime']/1000,
                            'pay_price'=>$v['totalCost'],
                            'consume_type'=>2,
                            'cps_type'=>2,
                            'goods_id'=>$v['detailList'][0]['goodsId'],
                            'commission'=>$commission,
                            'total_commission'=>$promotion_amount,
                            'order_status_name'=>'已付款'
                        ];

                        $consume_log_model->save($consume_log_data);
                        User::update(['is_buy'=>1],['id'=>$user_id]);
                        $user_model = new User();
                        $user_model->upgrade($user_id);

                        UserConsumeLog::lockMemberConsumeCalculate($user_id,$consume_log_model->total_commission,$consume_log_model->consume_id,0);
                        UserConsumeLog::newrecommendShopConsumeCalculate($user_id,0,$consume_log_model->total_commission,$consume_log_model->consume_id,2);
                    } else {
                        if ($v['status']==2&&$v['settled']==1&&$consume_log_info->is_to_account==0){
                            //发放消费记录的收益
                            UserConsumeLog::sendCommission($consume_log_info->order_no);
                        } elseif($v['status']==0&&$consume_log_info->is_refund==0) {
                            $consume_log_info->commission = 0;
                            $consume_log_info->order_status_name = '退款成功';
                            $consume_log_info->is_refund = 1;
                            $consume_log_info->save();
                            ExtendConsume::update(['commission'=>0],['consume_log_id'=>$consume_log_info->consume_id]);
                            ExtendConsumeShop::update(['commission'=>0],['consume_log_id'=>$consume_log_info->consume_id]);
                        }
                    }

                }
            }

            return $list;
        } else {
            return false;
            //throw new \Exception($result['msg']);
        }
    }
}